/**
 * 
 */
package com.smarthome.core.housemanager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShResidenceFeeService;
import com.smarthome.core.housemanager.ResidenceFeeService;
import com.smarthome.domain.bean.ShResidenceFee;

/**
 * @author dhc
 * @date 2015年9月23日 下午8:50:54
 * @title
 */
@Service("residenceFeeService")
public class ResidenceFeeServiceImpl implements ResidenceFeeService {

	@Resource(name="shResidenceFeeService")
	private ShResidenceFeeService shResidenceFeeService;


	@Override
	public ShResidenceFee residenceFeeNoNullByResidenceId(Long residenceId) {
		if(null == residenceId) return null;
		
		List<ShResidenceFee> rfs = shResidenceFeeService.selectEntryLisByResidenceId(residenceId);
		
		ShResidenceFee rf = new ShResidenceFee();
		if(null != rfs && rfs.size() > 0 ){
			rf = rfs.get(0)	;		
		} else {
			rf.setResidenceId(residenceId);
			rf.setBaseFee(0L);
			rf.setOtherFee(0l);
			shResidenceFeeService.insert(rf);
		}
		
		return rf;
	}

	@Override
	public ShResidenceFee residenceFeeByResidenceId(Long residenceId) {
		List<ShResidenceFee> rfs = shResidenceFeeService.selectEntryLisByResidenceId(residenceId);
		ShResidenceFee rf = new ShResidenceFee();
		if(null != rfs && rfs.size() > 0 ){
			rf = rfs.get(0)	;		
		}
		
		return rf;
	}

	@Override
	public ShResidenceFee residenceFeeById(Long id) {
		
		return shResidenceFeeService.selectEntryById(id);
	}

	@Override
	public int add(ShResidenceFee shResidenceFee) {
		return shResidenceFeeService.insert(shResidenceFee);
	}

	@Override
	public int edit(ShResidenceFee shResidenceFee) {
		Long id = shResidenceFee.getId();
		if(null == id) return 0;
		ShResidenceFee sv = shResidenceFeeService.selectEntryById(id);
		
		if(null == sv){
			return 0;
		}
		
		BeanUtils.copyProperties(shResidenceFee, sv);
		return shResidenceFeeService.saveOrUpdate(sv);
	}

}
